//
//  BGSlideView.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/18.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "BGSlideView.h"

#define kFontSize 14
#define kborderColor [kWhiteColor CGColor]

@interface BGSlideView ()
{
    CGSize _titleSize;
    NSString *_contentString;
    BOOL _isClick;
}
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic) double timeCount;
@end

@implementation BGSlideView

- (instancetype)initWithOrginY:(CGFloat)orginY content:(NSString *)content
{
    _titleSize = [content boundingRectWithSize:CGSizeMake(kScreenSize.width - 35, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize]} context:nil].size;
    
    _contentString = content;
    _isClick = NO;
    self = [super initWithFrame:CGRectMake(kScreenSize.width - 35, orginY, _titleSize.width, 30)];
    
    if (self) {
        [self creatCenterView];
    }
    return self;
}

//- (void)changeContent:(NSString *)content{
//    
//}

- (void)creatCenterView{
    self.backgroundColor = kClearColor;

    
    //图片右边 的文字
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(22, 3, _titleSize.width+23, 24)];
    label.text = _contentString;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = kWhiteColor;
    label.font = [UIFont systemFontOfSize:kFontSize];
//    kColorFrom0x(0x00FF00)
    label.backgroundColor = kGreenColor;
    label.layer.masksToBounds = YES;
    label.layer.cornerRadius = 5;
//    label.layer.borderWidth = 1;
//    label.layer.borderColor = kborderColor;
    [self addSubview:label];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//    kColorFrom0x(0x00FF00)
    backView.backgroundColor = kGreenColor;
    backView.layer.masksToBounds = YES;
    backView.layer.cornerRadius = 15;
    backView.layer.borderColor = kborderColor;
    backView.layer.borderWidth = 1;
    [self addSubview:backView];
    
    //添加图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 24, 24)];
    imageView.image = [UIImage imageNamed:@"shijianBai"];
    [backView addSubview:imageView];
    
    //图片上面的按钮
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _titleSize.width+23+30, self.sizeHeight)];
    [button setTitle:@"" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    
    [self buttonClick:nil];
    _timeCount = 3;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantPast]];
}

- (void)timeRun{
    _timeCount --;

    if (_timeCount < 1) {
        [self buttonClick:nil];
        [_timer setFireDate:[NSDate distantFuture]];
    }
}

- (void)buttonClick:(UIButton *)button{
    [UIView animateWithDuration:0.3 animations:^{
        if (_isClick) {
            self.frame = CGRectMake(kScreenSize.width - 35, self.orginY, _titleSize.width, 30);
        }else{
            self.frame = CGRectMake(kScreenSize.width - _titleSize.width - 30 - 17, self.orginY, _titleSize.width, 30);
        }
        _isClick = !_isClick;

    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
